# 使用官方.NET 8 SDK镜像进行构建
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# 复制所有源代码
COPY . .

# 清理任何现有的构建缓存
RUN find . -name "bin" -type d -exec rm -rf {} + || true
RUN find . -name "obj" -type d -exec rm -rf {} + || true

# 构建应用程序（包含还原和构建）
WORKDIR /src/src/RestaurantFinder.Web
RUN dotnet publish -c Release -o /app/publish

# 使用官方.NET 8 Runtime镜像运行应用
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app

# 安装curl用于健康检查
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# 创建非root用户
RUN groupadd -r appuser && useradd -r -g appuser appuser

# 复制发布的应用
COPY --from=build /app/publish .

# 设置权限
RUN chown -R appuser:appuser /app
USER appuser

# 暴露端口
EXPOSE 8080

# 设置环境变量
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production

# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:8080/health || exit 1

# 启动应用
ENTRYPOINT ["dotnet", "RestaurantFinder.Web.dll"] 